package com.goods.business.service.impl;

import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.ConsumerMapper;
import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.ConsummerService;
import com.goods.common.model.business.Consumer;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.Supplier;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ConsumerVO;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import net.sf.jsqlparser.expression.LongValue;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author jiaoyanlin
 * @Date 2021/11/8 19:53
 * @Description ConsummerServiceImpl
 */
@Service
public class ConsummerServiceImpl implements ConsummerService {
    @Autowired
    ConsumerMapper consumerMapper;
    @Override
    public PageVO<ConsumerVO> findConsumerList(Integer pageNum, Integer pageSize, Map<String,Object> map) {
        PageHelper.startPage(pageNum,pageSize);
        Object name = map.get("name");
        Object address = map.get("address");
        Object contact = map.get("contact");
        Example example =new Example(Consumer.class);
        Example.Criteria criteria = example.createCriteria();
        if (null!=name){
            String s = Convert.toStr(name);
            criteria.andLike("name","%"+s+"%");
        }

      if (null!=address){
            String address1=Convert.toStr(address);
            criteria.andLike("address","%"+address1+"%");
        }
        if (null!=contact){
            String contact1=Convert.toStr(contact);
            criteria.andLike("contact","%"+contact1+"%");
        }
        List<Consumer> consumers = consumerMapper.selectByExample(example);
         List<ConsumerVO> consumerVOS=new ArrayList<>();
        if (!CollectionUtils.isEmpty(consumers)){
            for (Consumer consumer : consumers) {
                ConsumerVO consumerVO = new ConsumerVO();
                BeanUtils.copyProperties(consumer,consumerVO);
                consumerVOS.add(consumerVO);
            }
            List<ConsumerVO> page = ListPageUtils.page(consumerVOS, pageSize, pageNum);
            PageInfo info = new PageInfo<>(page);
            return new PageVO<>(info.getTotal(),consumerVOS);
        }

        return null;

    }

    @Override
    public Boolean add(ConsumerVO consumerVO) {
        Consumer consumer = new Consumer();
        BeanUtils.copyProperties(consumerVO,consumer);
       /* Example example =new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
       */
        try {
            consumerMapper.insert(consumer);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    @Override
    public ConsumerVO edit(Long id) {
        Consumer consumer = consumerMapper.selectByPrimaryKey(id);
        ConsumerVO consumerVO = new ConsumerVO();
        BeanUtils.copyProperties(consumer,consumerVO);
        return consumerVO;
    }

    @Override
    public Boolean update(ConsumerVO consumerVO,Long id) {
        Consumer consumer = new Consumer();
        consumerVO.setId(id);
        BeanUtils.copyProperties(consumerVO,consumer);
        try {
            consumerMapper.updateByPrimaryKey(consumer);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    @Override
    public void delete(Long id) {
        consumerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageVO<ConsumerVO> findAll() {
        List<Consumer> consumers = consumerMapper.selectAll();
        List<ConsumerVO> consumerVOS = consumers.stream().map(consumer -> {
            ConsumerVO consumerVO = new ConsumerVO();
            BeanUtils.copyProperties(consumer, consumerVO);
            return consumerVO;
        }).collect(Collectors.toList());

        return new PageVO<>(consumerVOS.size(),consumerVOS);

    }

    @Override
    public List<ConsumerVO> findAlls() {
        List<Consumer> consumers = consumerMapper.selectAll();
        return consumers.stream().map(consumer -> {
            ConsumerVO consumerVO = new ConsumerVO();
            BeanUtils.copyProperties(consumer,consumerVO);
            return consumerVO;
        }).collect(Collectors.toList());

    }


}
